//
//  SEGActivityDetailRuleCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 2017/8/1.
//  Copyright © 2017年 陈区. All rights reserved.
//

#import "SEGActivityDetailRuleCell.h"

@interface SEGActivityDetailRuleCell ()

/**
 我上传的按钮
 */
@property (nonatomic, strong)UIButton *myUploadsButton;
/**
 规则描述Label
 */
@property (nonatomic, strong)UILabel *describleLabel;

/**
 底部箭头
 */
@property (nonatomic, strong)UIButton *arrowButton;

@end

@implementation SEGActivityDetailRuleCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupUI];
    }
    return self;
}

- (void)setupUI
{
    self.backgroundColor = kColor_W;
    
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.font = kFont_13;
    titleLabel.textColor = kColor_2;
    titleLabel.text = @"规则说明";
    [self.contentView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.top.equalTo(self.contentView);
        make.height.mas_equalTo(44);
    }];
    
    //我上传的按钮
    UIButton *myUploadsButton = [[UIButton alloc] init];
    [myUploadsButton setTitle:@"我发布的" forState:UIControlStateNormal];
    [myUploadsButton setTitleColor:kColor_T forState:UIControlStateNormal];
    myUploadsButton.titleLabel.font = kFont_13;
    myUploadsButton.layer.masksToBounds = YES;
    myUploadsButton.layer.cornerRadius = 11.5;
    myUploadsButton.layer.borderWidth = 1;
    myUploadsButton.layer.borderColor = kColor_T.CGColor;
    [myUploadsButton addTarget:self action:@selector(seg_myUploadsButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:myUploadsButton];
    [myUploadsButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(titleLabel);
        make.right.equalTo(self.contentView.mas_right).offset(-15);
        make.width.mas_equalTo(68);
        make.height.mas_equalTo(23);
    }];
    self.myUploadsButton = myUploadsButton;
    
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = kColor_L2;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.right.equalTo(self.contentView);
        make.top.equalTo(titleLabel.mas_bottom);
        make.height.mas_equalTo(0.5);
    }];
    
    UILabel *lable = [[UILabel alloc] init];
    lable.font = kFont_13;
    [self.contentView addSubview:lable];
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).offset(15);
        make.right.equalTo(self.contentView).offset(-15);
        make.top.equalTo(line.mas_bottom).offset(15);
    }];
    lable.numberOfLines = 0;
    self.describleLabel = lable;

    UIButton *arrowButton = [[UIButton alloc] init];
    [arrowButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_down")] forState:UIControlStateNormal];
    [arrowButton setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_list_up")] forState:UIControlStateSelected];
    [arrowButton addTarget:self action:@selector(seg_arrowButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:arrowButton];
    [arrowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(lable.mas_bottom).offset(0);
        make.width.mas_equalTo(36.5);
        make.height.mas_equalTo(36.5);//图片高度6.5 上下间距各15
    }];
    self.arrowButton = arrowButton;
}

- (void)setUpCellWithDescription:(NSString *)description isCollectVC:(BOOL)isCollectVC
{
    if (isCollectVC) {
        self.myUploadsButton.hidden = NO;
    }else {
        self.myUploadsButton.hidden = YES;
    }
    //过滤的特殊格式
    NSMutableAttributedString* attributedString = [[NSMutableAttributedString alloc] initWithData:[description dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute : NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    self.describleLabel.attributedText = attributedString;

    // 判断行数超过五行显示箭头
    self.describleLabel.numberOfLines = 0;
    CGFloat labelHeight = [self.describleLabel sizeThatFits:CGSizeMake(LYScreenWidth() - 30, MAXFLOAT)].height;
    CGFloat lineHeight = 15;//self.describleLabel.font.lineHeight;
    CGFloat count = labelHeight / lineHeight;
    if (count <= 5) {//不超过五行直接显示文字 不显示按钮 不限制行数
        self.describleLabel.numberOfLines = 0;
        self.arrowButton.hidden = YES;
        self.arrowButton.selected = NO;//还原按钮状态
        [self.describleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-15);
        }];
    }else {//超过五行 显示按钮
        if (self.arrowButton.selected) {//已展开 不限制行数
            self.describleLabel.numberOfLines = 0;
        }else {
            self.describleLabel.numberOfLines = 5;
        }
        self.arrowButton.hidden = NO;
        [self.describleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-37);
        }];
    }
    
}

- (void)seg_myUploadsButtonPressed
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(myUploadbuttonAction)]) {
        [self.delegate myUploadbuttonAction];
    }
}

- (void)seg_arrowButtonPressed
{
    self.arrowButton.selected = !self.arrowButton.selected;
    if (self.delegate && [self.delegate respondsToSelector:@selector(changeRuleViewHeight)]) {
        [self.delegate changeRuleViewHeight];
    }
}
@end
